#!/usr/bin/env groovy
// MILESTONE构建
def fExist(folder, fName){ def f = new File(folder, fName); if(f.exists()){return 'exist';}else{return 'none';}}

pipeline {
    agent {
        label 'master'
    }
    parameters {
        choice(name: 'db', choices: ['tgraph_kernel', 'postgresql', 'mariadb', 'neo4j1', 'neo4j2'], description: 'The DB to Test')
        choice(name: 'dataset', choices: ['energy', 'traffic', 'syn'], description: 'Datasets')
        string(name: 'tp_size', defaultValue: 'eu7d_20120101-20120108')
        booleanParam(name: 'rebuild', defaultValue: false, description: '强制重新构建milestone（已有的会被覆盖）')
    }
    tools {
        maven 'maven'
        jdk   'jdk8'
    }
    environment {
        PGPASSWORD = "langduhua"
        DATA_PATH = "D:\\tgraph\\data"
        SSD_WORKSPACE = "G:\\jenkins-workspace"
        NEO4J_LOG_DIR = "R:\\${params.db}"
        ARTIFACT_DIR = "D:\\tgraph\\jenkins"
        MILESTONE_NAME = "milestone_${params.db}_${params.tp_size}"
        MILESTONE_FULL_PATH = "${ARTIFACT_DIR}\\${env.MILESTONE_NAME}"
        MAVEN_OPTS='-Xmx12g -Xms1g'
    }
    stages {
        // 编译构建代码
        stage('编译') {
            when {
                beforeAgent true
                expression { currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause').size() > 0 }
            }
            steps{
                echo "Compile & building test client......"
                dir(env.SSD_WORKSPACE){
                    // dir('temporal-storage')
                    // {
                    //     git branch: 'TGraph2.3latest', credentialsId: 'songPC', url: 'git@gitee.com:tgraphdb/temporal-storage.git'
                    //     bat "mvn -B -Dmaven.test.skip clean install"
                    // }
                    // dir('temporal-neo4j')
                    // {
                    //     git branch: 'TGraph2.3latest', credentialsId: 'songPC', url: 'git@gitee.com:tgraphdb/temporal-neo4j.git'
                    //     // bat "mvn -B -DskipTests -Dlicense.skip -Dlicensing.skip clean install -pl org.neo4j:neo4j-kernel -am"
                    //     bat "mvn -B -Dmaven.test.skip -Dlicense.skip -Dlicensing.skip clean install -pl org.neo4j:neo4j-cypher -am"
                    // }
                    dir('demo-test')
                    {
                        git branch: 'dev-sjh', credentialsId: 'songPC', url: 'git@gitee.com:tgraphdb/demo-test.git'
                        bat "mvn -B -Dmaven.test.skip clean compile"
                    }
                }
                // script{
                    // def isStartedByUser = currentBuild.getBuildCauses('hudson.model.Cause$UpstreamCause')
                // }
            }
        }
        stage('清除当前数据库') {
            steps {
                script {
                    currentBuild.description = "${currentBuild.description==null?'':currentBuild.description} @${params.db}"
                    // 清除当前数据库，恢复milestone状态为空
                    switch (params.db) {
                        case 'postgresql':
                            // input "请在${env.NODE_NAME}上执行命令：
                            try {
                                // bat "dropdb.exe -Upostgres ${params.dataset}"
                                bat "echo DROP DATABASE IF EXISTS ${params.dataset}; | psql -Upostgres"
                            } catch (err) {
                                echo err
                            }
                            break
                        case 'mariadb':
                            bat "echo DROP DATABASE IF EXISTS ${params.dataset}; | mysql -u root -plangduhua"
                            break
                        case 'neo4j1':
                        case 'neo4j2':
                        case 'tgraph_kernel':
                            dir(env.SSD_WORKSPACE){
                                bat "if exist ${params.db} ( rmdir /q /s ${params.db} )"
                            }
                            dir(env.NEO4J_LOG_DIR){
                                bat "del neostore.transaction.db.*"
                            }
                            break
                    }
                }
            }
        }
        stage('恢复Milestone') {
            environment {
                MARIADB_WORKSPACE = "G:\\Program Files\\MariaDB 10.6\\data"
            }
            when {
                beforeAgent true
                allOf {
                    expression { !params.rebuild }
                    expression {
                        fExist(env.ARTIFACT_DIR, env.MILESTONE_NAME) == 'exist'
                    }
                }
            }
            steps {
                script {
                    // 恢复milestone状态为指定内容
                    switch (params.db) {
                        case 'postgresql':
                            bat "createdb.exe -Upostgres ${params.dataset}"
                            bat "pg_restore.exe -Upostgres -e -d ${params.dataset} ${env.MILESTONE_FULL_PATH}"
                            break
                        case 'mariadb':
                            dir(env.ARTIFACT_DIR){
                                unzip zipFile: "${env.MILESTONE_NAME}", dir: "${env.MILESTONE_NAME}_d"
                            }
                            bat "mariabackup.exe --prepare --target-dir=${env.MILESTONE_FULL_PATH}_d --user=root"
                            // 还原数据（删除datadir目录中与之冲突的文件即可）
                            bat "net stop MariaDB"
                            dir(env.MARIADB_WORKSPACE){
                                bat "del aria_log*"
                                bat "del ib*"
                                bat "del x*"
                                bat "if exist ${params.db} ( rmdir /q /s ${params.db} )"
                            }
                            bat "mariabackup.exe --copy-back --force-non-empty-directories --target-dir=${env.MILESTONE_FULL_PATH}_d --user=root"
                            bat "net start MariaDB"
                            break
                        case 'neo4j1':
                        case 'neo4j2':
                        case 'tgraph_kernel':
                            dir(env.SSD_WORKSPACE){
                                unzip zipFile: "${env.MILESTONE_FULL_PATH}", dir: "${params.db}"
                            }
                            break
                    }
                }
            }
            post{
                failure {
                    script{
                        try{
                            bat "net start MariaDB"
                        }catch(Exception e){
                            echo 'MariaDB started'
                        }
                    }
                }
            }
        }
        stage('构建milestone') {
            environment {
                DB_TYPE = "${params.db}"
                DATASET = "${params.dataset}"
            }
            when {
                beforeAgent true
                anyOf {
                    expression { params.rebuild }
                    expression {
                        fExist(env.ARTIFACT_DIR, env.MILESTONE_NAME) != 'exist'
                    }
                }
            }
            failFast true
            parallel {
                stage('Client'){
                    environment {
                        DB_TYPE = "${params.db}"
                        DATASET = "${params.dataset}"
                        TP_RANGE = "${params.tp_size}"
                        DB_HOST = "localhost"
                    }
                    steps {
                        echo "Build milestone of ${env.DB_TYPE} of ${env.DATASET} during ${env.TP_RANGE}"
                        echo "milestone file: ${env.MILESTONE_NAME}"
                        // 根据参数构建milestone
                        script {
                            dir(env.SSD_WORKSPACE){
                                dir('demo-test') {
                                    // for (i in ['first', 'second', 'third']) {
                                    //     try {
                                            sleep 10
                                            bat "mvn -B exec:java -Dexec.mainClass=edu.buaa.${params.dataset}.MilestoneBuilder -Dexec.cleanupDaemonThreads=false"
                                    //         bat "mvn -B exec:java -Dtest=edu.buaa.${params.dataset}.MilestoneBuilder"
                                    //         break
                                    //     } catch (err) {
                                    //         echo "tried ${i} time but server not started yet."
                                    //         if (i == 'third') {
                                    //             currentBuild.result = 'FAILURE'
                                    //             error 'ERR: Server NOT response.'
                                    //         }
                                    //     }
                                    // }
                                }
                            }
                        }
                        echo 'Client_Milestone_Finish_Amitabha'
                    }
                }
                stage('Watchdog'){
                    agent {
                        label 'master'
                    }
                    environment {
                        TIMEOUT = "300"
                        LOG_PIPE = "${currentBuild.rawBuild.getLogFile().getAbsolutePath()}"
                        MARKERS = 'Client_Milestone_Finish_Amitabha,Server_Milestone_Finish_Amitabha'
                    }
                    steps {
                        echo "Watch dog start on: ${env.LOG_PIPE}"
                        sleep 20
                        dir('G:\\jenkins-workspace\\demo-test') {
                            bat "mvn -B exec:java -Dexec.mainClass=edu.buaa.utils.TimeoutWatchDog -Dexec.cleanupDaemonThreads=false"
                        }
                    }
                }
                stage('Server'){
                    // when {
                    //     beforeAgent true
                    //     allOf {
                    //         expression { params.db != 'postgresql' }
                    //         expression { params.db != 'mariadb' }
                    //     }
                    // }
                    environment {
                        DB_PATH = "${env.SSD_WORKSPACE}\\${params.db}"
                    }
                    steps {
                        echo "DB_PATH: ${env.DB_PATH}"
                        script {
                            dir(env.SSD_WORKSPACE){
                                dir('demo-test') {
                                    switch (params.db) {
                                        case 'neo4j1':
                                            bat "mvn -B exec:java -Dexec.mainClass=edu.buaa.${params.dataset}.server.Neo4jServer1 -Dexec.cleanupDaemonThreads=false"
                                            break
                                        case 'neo4j2':
                                            bat "mvn -B exec:java -Dexec.mainClass=edu.buaa.${params.dataset}.server.Neo4jServer2 -Dexec.cleanupDaemonThreads=false"
                                            break
                                        case 'tgraph_kernel':
                                            bat "mvn -B exec:java -Dexec.mainClass=edu.buaa.${params.dataset}.server.TGraphKernelServer -Dexec.cleanupDaemonThreads=false"
                                            break
                                    }
                                }
                            }
                            switch (params.db) {
                                case 'neo4j1':
                                case 'neo4j2':
                                case 'tgraph_kernel':
                                    dir(env.NEO4J_LOG_DIR){
                                        bat 'del neostore.transaction.db.*'
                                    }
                            }
                        }
                        echo 'Server_Milestone_Finish_Amitabha'
                    }
                }
            }
            post{
                success {
                    echo 'success post after parallel'
                    script {
                        switch (params.db) {
                            case 'postgresql':
                                bat "pg_dump.exe -Upostgres --format=c --blobs --file=${env.MILESTONE_FULL_PATH} ${params.dataset}"
                                break
                            case 'mariadb':
                                bat "if exist ${env.MILESTONE_FULL_PATH}_d ( rmdir /q /s ${env.MILESTONE_FULL_PATH}_d )"
                                bat "mkdir ${env.MILESTONE_FULL_PATH}_d"
                                bat "mariabackup.exe --backup --target-dir=${env.MILESTONE_FULL_PATH}_d --user=root --password=langduhua --databases=${params.dataset}"
                                dir(env.ARTIFACT_DIR){
                                    zip zipFile: "${env.MILESTONE_NAME}", archive: false, dir: "${env.MILESTONE_NAME}_d", overwrite: true
                                    bat "rmdir /q /s ${env.MILESTONE_FULL_PATH}_d"
                                }
                                break
                            case 'neo4j1':
                            case 'neo4j2':
                            case 'tgraph_kernel':
                                dir(env.SSD_WORKSPACE){
                                    zip dir: "${params.db}", zipFile: "${env.MILESTONE_FULL_PATH}", overwrite: true, archive: false
                                    // bat "rmdir /q /s ${params.db}"
                                    // bat "xcopy ${params.db} ${env.MILESTONE_FULL_PATH} /y /e /h /i"
                                    // bat "ren ${params.db} ${env.MILESTONE_NAME}"
                                }
                            default:
                                error 'ERROR: should not happen!'
                        }
                    }
                    ws(env.ARTIFACT_DIR){
                        echo "search artifact ${env.MILESTONE_NAME} in ${env.ARTIFACT_DIR}"
                        archiveArtifacts artifacts: "${env.MILESTONE_NAME}", fingerprint: true, followSymlinks: false, onlyIfSuccessful: true
                    }
                }
            }
        }
        stage('统计大小'){
            environment {
                DB_TYPE = "${params.db}"
                DATASET = "${params.dataset}"
                DB_HOST = "localhost"
                DB_PATH = "${env.SSD_WORKSPACE}\\${params.db}"
                TP_RANGE = "${params.tp_size}"
            }
            steps{
                dir(env.SSD_WORKSPACE){
                    dir('demo-test') {
                        bat "mvn -B exec:java -Dexec.mainClass=edu.buaa.utils.DBSpaceStatistic -Dexec.cleanupDaemonThreads=false"
                    }
                }
            }
        }
    }
}